﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Benefit.Service.ServerData
{
    public class ServiceT_Account : Benefit.Interface.ServerData.IT_Account
    {
        Benefit.DB.DBManager db = null;
        public ServiceT_Account(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        /// <summary>
        /// 获取所有平台的虚拟账户
        /// </summary>
        /// <returns></returns>
        public List<Models.ServerData.T_Account> GetList()
        {
            List<Models.ServerData.T_Account> accounts = new List<Models.ServerData.T_Account>();
            string sql = "select * from T_Account Order by LoginAccount ";
            DavidDB.ADO.DBManager dbmanager = new DavidDB.ADO.DBManager();
            dynamic sdr = dbmanager.ExecuteReader(sql);
            while (sdr.Read())
            {
                Models.ServerData.T_Account c = new Models.ServerData.T_Account();
                //if (sdr["AppendMarginRate"] != null)
                //{
                //    c.AppendMarginRate = Convert.ToDouble(sdr["AppendMarginRate"]);
                //}
                c.CustomeId = Convert.ToInt32(sdr["CustomeId"]);
                c.DynamicPassword = Convert.ToString(sdr["DynamicPassword"]);
                //c.ForceClose = Convert.ToDouble(sdr["ForceClose"]);
                c.Id = Convert.ToInt32(sdr["Id"]);
                c.Isdeleted = Convert.ToInt32(sdr["Isdeleted"]);
                c.LoginAccount = Convert.ToString(sdr["LoginAccount"]);
                c.MaximumWithdrawals = Convert.ToInt32(sdr["MaximumWithdrawals"]);
                c.Password = sdr["Password"].ToString();
                c.TotalPosCost = Convert.ToDouble(sdr["TotalPosCost"]);
                c.TotalPositions = Convert.ToInt32(sdr["TotalPositions"]);
                accounts.Add(c);
            }
            sdr.Close();
            dbmanager.Commit();
            dbmanager.Dispose();
            return accounts;
        }

        /// <summary>
        /// 获取所有平台的虚拟账户
        /// </summary>
        /// <returns></returns>
        public List<Models.ServerData.T_Account> GetUnDeletedAccounts()
        {
            List<Models.ServerData.T_Account> accounts = new List<Models.ServerData.T_Account>();
            string sql = "select * from T_Account where Isdeleted=0 Order by LoginAccount";
            DavidDB.ADO.DBManager dbmanager = new DavidDB.ADO.DBManager();
            dynamic sdr = dbmanager.ExecuteReader(sql);
            while (sdr.Read())
            {
                Models.ServerData.T_Account c = new Models.ServerData.T_Account();
                c.AppendMarginRate = Convert.ToDouble(sdr["AppendMarginRate"]);
                c.CustomeId = Convert.ToInt32(sdr["CustomeId"]);
                c.DynamicPassword = Convert.ToString(sdr["DynamicPassword"]);
                c.ForceClose = Convert.ToDouble(sdr["ForceClose"]);
                c.Id = Convert.ToInt32(sdr["Id"]);
                c.Isdeleted = Convert.ToInt32(sdr["Isdeleted"]);
                c.LoginAccount = Convert.ToString(sdr["LoginAccount"]);
                c.MaximumWithdrawals = Convert.ToInt32(sdr["MaximumWithdrawals"]);
                c.Password = sdr["Password"].ToString();
                c.TotalPosCost = Convert.ToDouble(sdr["TotalPosCost"]);
                c.TotalPositions = Convert.ToInt32(sdr["TotalPositions"]);
                accounts.Add(c);
            }
            sdr.Close();
            dbmanager.Commit();
            dbmanager.Dispose();
            return accounts;
        }
        /// <summary>
        /// 判断分析系统的账户数量是否和平台一致
        /// </summary>
        /// <returns></returns>
        public bool AccountCountIsSameToBenefit()
        {
            bool accountCountIsSameToBenefit = false;
            Interface.ServerData.IT_Account ita = new ServerData.ServiceT_Account(db);
            List<Models.ServerData.T_Account> serverAccounts = ita.GetList();
            var query = db.Accounts;
            int accountCount = query.Count();
            if (accountCount > 0)
            {
                if (accountCount == serverAccounts.Count)
                {
                    accountCountIsSameToBenefit = true;
                }
            }
            return accountCountIsSameToBenefit;
        }


        #region 得到还没有在分析系统存在的账号
        /// <summary>
        /// 得到还没有在分析系统存在的账号
        /// </summary>
        /// <returns></returns>
        public List<Models.ServerData.T_Account> GetUnRegistAccounts()
        {
            List<Models.ServerData.T_Account> accounts = new List<Models.ServerData.T_Account>();//保存还未注册的账户
            Interface.ServerData.IT_Account ita = new ServerData.ServiceT_Account(db);
            List<Models.ServerData.T_Account> serverAccounts = ita.GetList();//获取所有账户
            foreach (Models.Sys.Account account in db.Accounts)
            {
                var query = serverAccounts.Where(a => a.LoginAccount.Equals(account.AccountName));
                if (query.First().Isdeleted == 1)
                {
                    account.IsDeleted = true;
                }
            }
            db.SaveChanges();
            //   List<Sys.Account> benefitaccounts = new Sys.Account().GetList();
            foreach (Models.ServerData.T_Account t in serverAccounts)
            {

                var query = db.Accounts.Where(a => a.AccountName.Equals(t.LoginAccount));
                //   var query = from s in benefitaccounts where t.LoginAccount.Equals(s.AccountName) select s;
                if (query.Count() > 0)//曾经写入过
                {
                    if (query.First().IsDeleted == false && t.Isdeleted == 1)
                    {
                        query.First().IsDeleted = true;
                    }
                }
                else
                {
                    accounts.Add(t);
                }
            }

            db.SaveChanges();
            return accounts;
        }
        #endregion

        public Models.ServerData.T_Account GetModel(int id)
        {
            DavidDB.ADO.DBManager dbmanager = new DavidDB.ADO.DBManager();
            Models.ServerData.T_Account account = new Models.ServerData.T_Account();
            account.Id = id;
            account = (Models.ServerData.T_Account)dbmanager.GetModel(account);
            dbmanager.Commit();
            dbmanager.Dispose();
            return account;
        }
    }
}